package com.abel.bigwater.tlv.model

import java.time.LocalDateTime

enum class BaseParam(var id: Short = 0,
                     var len: Short = 0,
                     var field: String = "",
                     var desc: String = "") {
    T0IMSI(0, 15, "IMSI号", "15位IMSI号，用ASCII码表示"),
    T1DevType(1, 1, "设备类型", "1:水表"),
    T2Status(2, 1, "状态", "1:已注册，0:未注册，见附录"),
    T3MeterCode(3, 7, "水表表号", "BCD"),
    T4TermClock(4, 6, "终端时钟", "YYMMDDHHMMSS"),
    T5TermRun(5, 2, "终端运行时间", "天"),
    T6TermVersion(6, 1, "终端软件版本", "");

    companion object {
        val TAG: Short = 1

        val valueMap = HashMap<Short, BaseParam>()

        init {
            BaseParam.values().forEach { valueMap[it.id] = it }
        }
    }
}

enum class RealtimeParam(var id: Short = 0,
                         var len: Short = 0,
                         var field: String = "",
                         var desc: String = "") {
    T0RealTotal(0, 4, "实时累计流量", "单位升, 如果时间间隔大于24小时，首流量时间当日24点表头读数，如果时间间隔小于24，则发送当前的读数"),
    T1DayTotal(1, 4, "日结累计流量", "单位升，首流量时间当日正流量累加，如果时间间隔小于24，则发送当日0点到发送时间的正流量"),
    T2DayRevertTotal(2, 4, "日结累计逆流量", "单位升，首流量时间当日负流量累加，如果时间间隔小于24，则发送当日0点到发送时间的负流量"),
    T3DayMaxFlow(3, 2, "日最高流速", "单位0.1L/S，5min采一次"),
    T4DayMaxTime(4, 3, "最高流速时间戳", "格式为HHMMSS"),
    T5Signal(5, 6, "信号参数", """实时的信号强度+信噪比+覆盖等级，各BYTE[2]；
信号强度：RSRP（电信标准）+140
信噪比：SNR
覆盖等级：0，1，2"""),
    T6Volt(6, 2, "电池电压", "0.01V为单位"),
    T7Pulse(7, 2, "脉冲当量", "以L为单位");

    companion object {
        val TAG: Short = 2

        val valueMap = HashMap<Short, RealtimeParam>()

        init {
            RealtimeParam.values().forEach { valueMap[it.id] = it }
        }
    }
}

enum class TerminalParam(var id: Short = 0,
                         var len: Short = 0,
                         var field: String = "",
                         var desc: String = "") {
    T0VoltWarn(0x0, 2, "电压报警阀值", "0.01V"),
    T1MaxFlowWarn(0x1, 4, "大流量报警阀值", "升，基表15口径的4000 L/h，20与25口径的5000 L/h"),
    T2MaxFlowDuration(0x2, 1, "持续高流量持续时间", "0~255，单位10分钟"),
    T3ServerIp(0x3, 6, "服务器ip地址", "服务器ip地址、端口"),
    T4PeriodStart(0x4, 6, "周期上报起始时间", "格式为YYMMDDHHMMSS"),
    T5PeriodEnd(0x5, 6, "周期上报结束时间", "格式为YYMMDDHHMMSS"),
    T6PeriodInterval(0x6, 1, "周期上报数据间隔参数", "默认30分钟，支持15分钟，可以到5分钟"),
    T7PeriodFreq(0x7, 1, "周期上报频率", "默认24小时"),
    T8UpRetryTimes(0x8, 1, "上行链接重试次数", "默认3次，最大5次"),
    T9MaxRetryPeriod(0x9, 1, "最大重发周期", "默认20分钟"),
    TAAesKey(0xA, 16, "SN", "AES加密用的key，ascii"),
    TBWarnId(0xB, 2, "即时报警ID", "用16位表示所有告警（位域代表的告警参见附录），位域值为1表示此告警需要唤醒水表立即上报，0表示此告警在日报里一起上报"),
    TCApnInfo(0xC, 10, "APN信息", "见附录"),
    TDReset(0xD, 1, "恢复出厂设置", "1：恢复出厂设置"),
    TEBaseReading(0xE, 4, "基表流量", "水表底数；基表出厂时的读数");

    companion object {
        val TAG: Short = 3

        val valueMap = HashMap<Short, TerminalParam>()

        init {
            TerminalParam.values().forEach { valueMap[it.id] = it }
        }
    }
}

enum class WarnParam(var id: Short = 0,
                     var len: Short = 0,
                     var field: String = "",
                     var desc: String = "") {
    T1MaxFlowWarn(1, 8, "大流量报警", "持续30min报警，大于大流量阀值，平台内持续高流量持续时间参数*10"),
    T2DataTamper(2, 1, "数据被篡改", "1：篡改，被如下数据被修改时产生此告警：序列号，生产日期，累计流量，0不报警"),
    T3RevertWarn(3, 1, "反流告警", "连续T7分钟内反流流速超过RevFlowThr (L/Min)，1报警，0不报警"),
    T4Magnetic(4, 1, "磁干扰", "1报警，0不报警"),
    T5InternalError(5, 1, "内部错误", "水表厂家自行定义"),
    T6RemoteDetach(6, 1, "远传模块分离", "1，远传模块与基表分离");

    companion object {

        val TAG: Short = 4

        val valueMap = HashMap<Short, WarnParam>()

        init {
            WarnParam.values().forEach { valueMap[it.id] = it }
        }
    }
}

/**
 * 大流量报警参数

字段	数据类型	描述及要求
大流量开始时间	BYTE[6]	格式为YYMMDDHHMMSS
最大流速	BYTE[2]	单位L/H
 */
data class MaxFlowWarn(var maxFlowTimestamp: ByteArray = ByteArray(6),
                       var maxFlow: Int = 0)

enum class ResultCodeParam(var id: Short = 0,
                           var len: Short = 0,
                           var field: String = "",
                           var desc: String = "") {
    T0ResultCode(0, 1, "结果码", "");

    companion object {
        val TAG: Short = 5
    }
}

enum class ResultCodeType(var id: Short = 0,
                          var desc: String = "") {
    T0Success(0, "处理成功"),
    T1Bad(1, "消息有误"),
    T2NotSupport(2, "不支持"),
    T3Fail(3, "处理失败"),
    T255Other(255, "其它-预留");
}

data class PeriodDataParam(var startClock: LocalDateTime? = null,
                           var period: Short = 0,
                           var count: Short = 0,
                           /**
                            * 时间周期内瞬时流量，如果24小时，则48个周期数据，如果上报周期小于24，
                            * 则上报当日0点到上报时间的周期数据，例如在2点，上报4个数据，到4点，
                            * 上报8个数据，如果上报周期隔天，则第一个周期上报前一天的所有数据，
                            * 第二个周期上报当前日的从0点开始计算的数据
                            */
                           var valueList: IntArray? = null) {
    companion object {
        val TAG: Short = 6
    }
}

enum class TlvCmdType(var cmd: Short = 0,
                      var reply: Short = 0,
                      var desc: String = "") {
    T1Online(1, 51, "终端上线"),
    T2SetTerminal(2, 52, "平台向终端设置参数"),
    T3UpRealtime(3, 53, "终端向平台发送实时数据"),
    T4QueryTerminal(4, 54, "平台向终端查询信息"),
    T5Warn(5, 55, "即时报警");

    companion object {
        val valueMap = HashMap<Short, TlvCmdType>()
        val replyMap = HashMap<Short, TlvCmdType>();

        init {
            TlvCmdType.values().forEach {
                valueMap[it.cmd] = it
                replyMap[it.reply] = it
            }
        }
    }
}

enum class MeterStatusParam(var resultCode: Int = 0,
                            var desc: String = "") {
    T0UnRegistered(0, "未注册"),
    T1Regisetered(1, "已注册"),
    T2Other(2, "其它-预留");

    companion object {
        val valueMap = HashMap<Int, MeterStatusParam>()

        init {
            MeterStatusParam.values().forEach { valueMap[it.resultCode] }
        }
    }
}
